//
//  WQSysAccessPermission.m
//  VideoDemo
//
//  Created by C on 2019/8/19.
//  Copyright © 2019 c. All rights reserved.
//

#import "WQSysAccessPermission.h"

@implementation WQSysAccessPermission

/**
 检测录制视频的相机是否授权
 */
+ (void)checkVideoCameraAuthorization:(void (^) (BOOL isGranted))callback
{
    __block BOOL isAvalible = NO;
    __weak __typeof__(self) weakSelf = self;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status) {
        case AVAuthorizationStatusAuthorized: //授权
            isAvalible = YES;
            break;
        case AVAuthorizationStatusDenied:   //拒绝，弹框
        {
            [self showWithMessage:@"此功能需要您授权本App打开相机\n设置方法:打开手机设置->隐私->相机"];
            isAvalible = NO;
        }
            break;
        case AVAuthorizationStatusNotDetermined:   //没有决定，第一次启动默认弹框
        {
            //点击弹框授权
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                isAvalible = granted;
                if(granted)
                {
                    isAvalible = YES;
                } else {
                    isAvalible = NO;
                    //如果不允许
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf gotoSetting];
                    });
                }
            }];
        }
            break;
        case AVAuthorizationStatusRestricted:  //受限制，家长控制器
            isAvalible = NO;
            break;
    }
    callback(isAvalible);;
}

/**
 检测录制视频的麦克风是否授权
 */
+ (void)checkAudioAuthorization:(void (^) (BOOL isGranted))callback
{
    __block BOOL isAvalible = NO;
    __weak __typeof__(self) weakSelf = self;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (status) {
        case AVAuthorizationStatusAuthorized: //授权
            isAvalible = YES;
            break;
        case AVAuthorizationStatusDenied:   //拒绝，弹框
        {
            [self showWithMessage:@"此功能需要您授权本App打开麦克风\n设置方法:打开手机设置->隐私->麦克风"];
            isAvalible = NO;
        }
            break;
        case AVAuthorizationStatusNotDetermined:   //没有决定，第一次启动
        {
            //点击弹框授权
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                isAvalible = granted;
                if(granted)
                {
                    isAvalible = YES;
                } else {
                    isAvalible = NO;
                    //如果不允许,回到主线程
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf gotoSetting];
                    });
                }
            }];
        }
            break;
        case AVAuthorizationStatusRestricted:  //受限制，家长控制器
            isAvalible = NO;
            break;
    }
    callback(isAvalible);
}

//MARK: - 提示

//弹框提示
+ (void)showWithMessage:(NSString *)tipString
{
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:tipString delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alter show];
}

//引导到设置页面去设置
+ (void)gotoSetting
{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        [[UIApplication sharedApplication]openURL:url];
    }
}

@end
